home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / SelfLoader.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  11.5 KB  |  291 lines

  1. package SelfLoader;
  2. use Carp;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(AUTOLOAD);
  6. $VERSION = 1.07; sub Version {$VERSION}
  7. $DEBUG = 0;
  8.  
  9. my %Cache;      # private cache for all SelfLoader's client packages
  10.  
  11. AUTOLOAD {
  12.     print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if $DEBUG;
  13.     my $SL_code = $Cache{$AUTOLOAD};
  14.     unless ($SL_code) {
  15.         $AUTOLOAD =~ m/^(.*)::/;
  16.         SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"};
  17.         $SL_code = $Cache{$AUTOLOAD};
  18.         $SL_code = "sub $AUTOLOAD { }"
  19.             if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/);
  20.         croak "Undefined subroutine $AUTOLOAD" unless $SL_code;
  21.     }
  22.     print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if $DEBUG;
  23.     eval $SL_code;
  24.     if ($@) {
  25.         $@ =~ s/ at .*\n//;
  26.         croak $@;
  27.     }
  28.     defined(&$AUTOLOAD) || die "SelfLoader inconsistency error";
  29.     delete $Cache{$AUTOLOAD};
  30.     goto &$AUTOLOAD
  31. }
  32.  
  33. sub load_stubs { shift->_load_stubs((caller)[0]) }
  34.  
  35. sub _load_stubs {
  36.     my($self, $callpack) = @_;
  37.     my $fh = \*{"${callpack}::DATA"};
  38.     my $currpack = $callpack;
  39.     my($line,$name,@lines, @stubs, $protoype);
  40.  
  41.     print STDERR "SelfLoader::load_stubs($callpack)\n" if $DEBUG;
  42.     croak("$callpack doesn't contain an __DATA__ token")
  43.         unless fileno($fh);
  44.     $Cache{"${currpack}::<DATA"} = 1;   # indicate package is cached
  45.  
  46.     while(defined($line = <$fh>) and $line !~ m/^__END__/) {
  47.         if ($line =~ m/^sub\s+([\w:]+)\s*(\([\\\$\@\%\&\*\;]*\))?/) {
  48.             push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
  49.             $protoype = $2;
  50.             @lines = ($line);
  51.             if (index($1,'::') == -1) {         # simple sub name
  52.                 $name = "${currpack}::$1";
  53.             } else {                            # sub name with package
  54.                 $name = $1;
  55.                 $name =~ m/^(.*)::/;
  56.                 if (defined(&{"${1}::AUTOLOAD"})) {
  57.                     \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
  58.                         die 'SelfLoader Error: attempt to specify Selfloading',
  59.                             " sub $name in non-selfloading module $1";
  60.                 } else {
  61.                     $self->export($1,'AUTOLOAD');
  62.                 }
  63.             }
  64.         } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared
  65.             push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
  66.             $self->_package_defined($line);
  67.             $name = '';
  68.             @lines = ();
  69.             $currpack = $1;
  70.             $Cache{"${currpack}::<DATA"} = 1;   # indicate package is cached
  71.             if (defined(&{"${1}::AUTOLOAD"})) {
  72.                 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
  73.                     die 'SelfLoader Error: attempt to specify Selfloading',
  74.                         " package $currpack which already has AUTOLOAD";
  75.             } else {
  76.                 $self->export($currpack,'AUTOLOAD');
  77.             }
  78.         } else {
  79.             push(@lines,$line);
  80.         }
  81.     }
  82.     close($fh) unless defined($line) && $line =~ /^__END__\s*DATA/;     # __END__
  83.     push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
  84.     eval join('', @stubs) if @stubs;
  85. }
  86.  
  87.  
  88. sub _add_to_cache {
  89.     my($self,$fullname,$pack,$lines, $protoype) = @_;
  90.     return () unless $fullname;
  91.     carp("Redefining sub $fullname") if exists $Cache{$fullname};
  92.     $Cache{$fullname} = join('', "package $pack; ",@$lines);
  93.     print STDERR "SelfLoader cached $fullname: $Cache{$fullname}" if $DEBUG;
  94.     defined($protoype) ? "sub $fullname $protoype;" : "sub $fullname;"
  95. }
  96.  
  97. sub _package_defined {}
  98.  
  99. 1;
  100. __END__
  101.  
  102. =head1 NAME
  103.  
  104. SelfLoader - load functions only on demand
  105.  
  106. =head1 SYNOPSIS
  107.  
  108.     package FOOBAR;
  109.     use SelfLoader;
  110.     
  111.     ... (initializing code)
  112.     
  113.     __DATA__
  114.     sub {....
  115.  
  116.  
  117. =head1 DESCRIPTION
  118.  
  119. This module tells its users that functions in the FOOBAR package are to be
  120. autoloaded from after the C<__DATA__> token.  See also
  121. L<perlsub/"Autoloading">.
  122.  
  123. =head2 The __DATA__ token
  124.  
  125. The C<__DATA__> token tells the perl compiler that the perl code
  126. for compilation is finished. Everything after the C<__DATA__> token
  127. is available for reading via the filehandle FOOBAR::DATA,
  128. where FOOBAR is the name of the current package when the C<__DATA__>
  129. token is reached. This works just the same as C<__END__> does in
  130. package 'main', but for other modules data after C<__END__> is not
  131. automatically retreivable , whereas data after C<__DATA__> is.
  132. The C<__DATA__> token is not recognized in versions of perl prior to
  133. 5.001m.
  134.  
  135. Note that it is possible to have C<__DATA__> tokens in the same package
  136. in multiple files, and that the last C<__DATA__> token in a given
  137. package that is encountered by the compiler is the one accessible
  138. by the filehandle. This also applies to C<__END__> and main, i.e. if
  139. the 'main' program has an C<__END__>, but a module 'require'd (_not_ 'use'd)
  140. by that program has a 'package main;' declaration followed by an 'C<__DATA__>',
  141. then the C<DATA> filehandle is set to access the data after the C<__DATA__>
  142. in the module, _not_ the data after the C<__END__> token in the 'main'
  143. program, since the compiler encounters the 'require'd file later.
  144.  
  145. =head2 SelfLoader autoloading
  146.  
  147. The B<SelfLoader> works by the user placing the C<__DATA__>
  148. token I<after> perl code which needs to be compiled and
  149. run at 'require' time, but I<before> subroutine declarations
  150. that can be loaded in later - usually because they may never
  151. be called.
  152.  
  153. The B<SelfLoader> will read from the FOOBAR::DATA filehandle to
  154. load in the data after C<__DATA__>, and load in any subroutine
  155. when it is called. The costs are the one-time parsing of the
  156. data after C<__DATA__>, and a load delay for the _first_
  157. call of any autoloaded function. The benefits (hopefully)
  158. are a speeded up compilation phase, with no need to load
  159. functions which are never used.
  160.  
  161. The B<SelfLoader> will stop reading from C<__DATA__> if
  162. it encounters the C<__END__> token - just as you would expect.
  163. If the C<__END__> token is present, and is followed by the
  164. token DATA, then the B<SelfLoader> leaves the FOOBAR::DATA
  165. filehandle open on the line after that token.
  166.  
  167. The B<SelfLoader> exports the C<AUTOLOAD> subroutine to the
  168. package using the B<SelfLoader>, and this loads the called
  169. subroutine when it is first called.
  170.  
  171. There is no advantage to putting subroutines which will _always_
  172. be called after the C<__DATA__> token.
  173.  
  174. =head2 Autoloading and package lexicals
  175.  
  176. A 'my $pack_lexical' statement makes the variable $pack_lexical
  177. local _only_ to the file up to the C<__DATA__> token. Subroutines
  178. declared elsewhere _cannot_ see these types of variables,
  179. just as if you declared subroutines in the package but in another
  180. file, they cannot see these variables.
  181.  
  182. So specifically, autoloaded functions cannot see package
  183. lexicals (this applies to both the B<SelfLoader> and the Autoloader).
  184. The C<vars> pragma provides an alternative to defining package-level
  185. globals that will be visible to autoloaded routines. See the documentation
  186. on B<vars> in the pragma section of L<perlmod>.
  187.  
  188. =head2 SelfLoader and AutoLoader
  189.  
  190. The B<SelfLoader> can replace the AutoLoader - just change 'use AutoLoader'
  191. to 'use SelfLoader' (though note that the B<SelfLoader> exports
  192. the AUTOLOAD function - but if you have your own AUTOLOAD and
  193. are using the AutoLoader too, you probably know what you're doing),
  194. and the C<__END__> token to C<__DATA__>. You will need perl version 5.001m
  195. or later to use this (version 5.001 with all patches up to patch m).
  196.  
  197. There is no need to inherit from the B<SelfLoader>.
  198.  
  199. The B<SelfLoader> works similarly to the AutoLoader, but picks up the
  200. subs from after the C<__DATA__> instead of in the 'lib/auto' directory.
  201. There is a maintainance gain in not needing to run AutoSplit on the module
  202. at installation, and a runtime gain in not needing to keep opening and
  203. closing files to load subs. There is a runtime loss in needing
  204. to parse the code after the C<__DATA__>. Details of the B<AutoLoader> and
  205. another view of these distinctions can be found in that module's
  206. documentation.
  207.  
  208. =head2 __DATA__, __END__, and the FOOBAR::DATA filehandle.
  209.  
  210. This section is only relevant if you want to use
  211. the C<FOOBAR::DATA> together with the B<SelfLoader>.
  212.  
  213. Data after the C<__DATA__> token in a module is read using the
  214. FOOBAR::DATA filehandle. C<__END__> can still be used to denote the end
  215. of the C<__DATA__> section if followed by the token DATA - this is supported
  216. by the B<SelfLoader>. The C<FOOBAR::DATA> filehandle is left open if an
  217. C<__END__> followed by a DATA is found, with the filehandle positioned at
  218. the start of the line after the C<__END__> token. If no C<__END__> token is
  219. present, or an C<__END__> token with no DATA token on the same line, then
  220. the filehandle is closed.
  221.  
  222. The B<SelfLoader> reads from wherever the current
  223. position of the C<FOOBAR::DATA> filehandle is, until the
  224. EOF or C<__END__>. This means that if you want to use
  225. that filehandle (and ONLY if you want to), you should either
  226.  
  227. 1. Put all your subroutine declarations immediately after
  228. the C<__DATA__> token and put your own data after those
  229. declarations, using the C<__END__> token to mark the end
  230. of subroutine declarations. You must also ensure that the B<SelfLoader>
  231. reads first by  calling 'SelfLoader-E<gt>load_stubs();', or by using a
  232. function which is selfloaded;
  233.  
  234. or
  235.  
  236. 2. You should read the C<FOOBAR::DATA> filehandle first, leaving
  237. the handle open and positioned at the first line of subroutine
  238. declarations.
  239.  
  240. You could conceivably do both.
  241.  
  242. =head2 Classes and inherited methods.
  243.  
  244. For modules which are not classes, this section is not relevant.
  245. This section is only relevant if you have methods which could
  246. be inherited.
  247.  
  248. A subroutine stub (or forward declaration) looks like
  249.  
  250.   sub stub;
  251.  
  252. i.e. it is a subroutine declaration without the body of the
  253. subroutine. For modules which are not classes, there is no real
  254. need for stubs as far as autoloading is concerned.
  255.  
  256. For modules which ARE classes, and need to handle inherited methods,
  257. stubs are needed to ensure that the method inheritance mechanism works
  258. properly. You can load the stubs into the module at 'require' time, by
  259. adding the statement 'SelfLoader-E<gt>load_stubs();' to the module to do
  260. this.
  261.  
  262. The alternative is to put the stubs in before the C<__DATA__> token BEFORE
  263. releasing the module, and for this purpose the C<Devel::SelfStubber>
  264. module is available.  However this does require the extra step of ensuring
  265. that the stubs are in the module. If this is done I strongly recommend
  266. that this is done BEFORE releasing the module - it should NOT be done
  267. at install time in general.
  268.  
  269. =head1 Multiple packages and fully qualified subroutine names
  270.  
  271. Subroutines in multiple packages within the same file are supported - but you
  272. should note that this requires exporting the C<SelfLoader::AUTOLOAD> to
  273. every package which requires it. This is done automatically by the
  274. B<SelfLoader> when it first loads the subs into the cache, but you should
  275. really specify it in the initialization before the C<__DATA__> by putting
  276. a 'use SelfLoader' statement in each package.
  277.  
  278. Fully qualified subroutine names are also supported. For example,
  279.  
  280.    __DATA__
  281.    sub foo::bar {23}
  282.    package baz;
  283.    sub dob {32}
  284.  
  285. will all be loaded correctly by the B<SelfLoader>, and the B<SelfLoader>
  286. will ensure that the packages 'foo' and 'baz' correctly have the
  287. B<SelfLoader> C<AUTOLOAD> method when the data after C<__DATA__> is first
  288. parsed.
  289.  
  290. =cut
  291.